
package com.stchou.superbrowser.ui.adapter;

import com.stchou.superbrowser.activity.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * image text adapter
 */
public class ImageTextAdapter extends BaseAdapter
{
    private View[] itemViews;

    /**
     * 载入图片 与 文字
     * @param mImageIds resId
     * @param TitleTexts text to show
     * @param context
     */
    public ImageTextAdapter(Context context,int[] mImageIds, int[] TitleTexts)
    {
        itemViews = new View[mImageIds.length];

        for (int i = 0; i < itemViews.length; i++)
        {
            itemViews[i] = makeItemView(mImageIds[i], TitleTexts[i], context);
        }
    }

    public int getCount()
    {
        return itemViews.length;
    }

    public View getItem(int position)
    {
        return itemViews[position];
    }

    public long getItemId(int position)
    {
        return position;
    }

    private View makeItemView(int strmImageIds, int strTitleTexts, Context context)
    {
        LayoutInflater inflater = (LayoutInflater)context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.menuitem, null);
        TextView title = (TextView)itemView.findViewById(R.id.TextItemId);
        title.setText(strTitleTexts);
        ImageView image = (ImageView)itemView.findViewById(R.id.ImageItemId);
        image.setImageResource(strmImageIds);
        image.setScaleType(ImageView.ScaleType.FIT_CENTER);

        return itemView;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
            return itemViews[position];
        return convertView;
    }
}
